package org.chimi.rr4s.rest;

import java.util.List;

import org.chimi.rr4s.domain.ItemRepository;
import org.chimi.rr4s.domain.ItemResult;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 
 * @author Choi, Beom Kyun
 * 
 */
@Controller
public class RecommendItemsApi {

	private ItemRepository itemRepository;

	@RequestMapping("/api/recommend/items/viewedtogether")
	@ResponseBody
	public RecommendItemResult recommendViewedTogether(
			@RequestParam("itemId") String itemId,
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "size", required = false, defaultValue = "15") Integer size) {
		List<ItemResult> itemResults = itemRepository.findViewedItemsTogether(
				itemId, new PageRequest(page, size));
		return new RecommendItemResult(itemResults);
	}

	@RequestMapping("/api/recommend/items/likedtogether")
	@ResponseBody
	public RecommendItemResult recommendLikedTogether(
			@RequestParam("itemId") String itemId,
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "size", required = false, defaultValue = "15") Integer size) {
		List<ItemResult> itemResults = itemRepository.findLikedItemsTogether(
				itemId, new PageRequest(page, size));
		return new RecommendItemResult(itemResults);
	}

	@RequestMapping("/api/recommend/items/boughttogether")
	@ResponseBody
	public RecommendItemResult recommendBoughtTogether(
			@RequestParam("itemId") String itemId,
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "size", required = false, defaultValue = "15") Integer size) {
		List<ItemResult> itemResults = itemRepository.findLikedItemsTogether(
				itemId, new PageRequest(page, size));
		return new RecommendItemResult(itemResults);
	}

	@RequestMapping("/api/recommend/items/otheralsoviewed")
	@ResponseBody
	public RecommendItemResult recommendOthersAlsoViewed(
			@RequestParam("userId") String userId,
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "size", required = false, defaultValue = "15") Integer size) {
		List<ItemResult> itemResults = itemRepository
				.findAlsoViewedItemsForUserById(userId, new PageRequest(page, size));
		return new RecommendItemResult(itemResults);
	}

	@RequestMapping("/api/recommend/items/otheralsoliked")
	@ResponseBody
	public RecommendItemResult recommendOthersAlsoLiked(
			@RequestParam("userId") String userId,
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "size", required = false, defaultValue = "15") Integer size) {
		List<ItemResult> itemResults = itemRepository
				.findAlsoLikedItemsForUserById(userId, new PageRequest(page, size));
		return new RecommendItemResult(itemResults);
	}

	@RequestMapping("/api/recommend/items/otheralsobought")
	@ResponseBody
	public RecommendItemResult recommendOthersAlsoBought(
			@RequestParam("userId") String userId,
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "size", required = false, defaultValue = "15") Integer size) {
		List<ItemResult> itemResults = itemRepository
				.findAlsoBoughtItemsForUserById(userId, new PageRequest(page, size));
		return new RecommendItemResult(itemResults);
	}

	public void setItemRepository(ItemRepository itemRepository) {
		this.itemRepository = itemRepository;
	}

}
